// Revision3.js for PSPTube
// Made By JK108

function Revision3_CheckURL( url, option )
{
	if (url.match("http://revision3.com") == "http://revision3.com"){
		return 1;
	} else{
		return 0;
	}
}

/*
 Get a Revision3 URL
*/
function Revision3_GetURL( url, option )
{
	var contents = GetContents(url);
	if(contents == null) {return null; /* failed to get page */ }
	var video_url = contents.match(/&amp;file\=(.*?)\"/);
	var video_url = video_url[1];
	return video_url;
}

function Revision3_Search( keyword, start_index, length, option )
{
	var result = new Object();
        result.keyword   = keyword;	// Search
	result.VideoInfo = new Array();	// Results in sequence
	result.start     = start_index;	// Index of first result
	result.end       = 0;
	result.total     = -1;
	var perpage = 10;


	result.start = (((result.start - 1) / 10) * perpage) + 1;


	if(result.start < perpage){
	var contents = GetContents( "http://revision3.com/search/search?q=" + keyword.replace(/ /g, "+") );
	} else{
	var num = result.start - 1;
	var contents = GetContents( "http://revision3.com/search/search?q=" + keyword.replace(/ /g, "%20") + "&start=" + num );
	}
	if(contents == null) { return null; /* failed to get page */ }


	if(contents.match("<p class=\"search-results-not-found\">") == "<p class=\"search-results-not-found\">"){ result.total = 0; return result; }
	result.total = 100000;


	while(1){
	var pos = contents.indexOf("<div class=\"ez-thumb\">");

	if(pos != -1){
		contents = contents.substring(pos + 22, contents.length);
		//Get URL
		var url = contents.match(/href\=\"(.*?)\"/);
		var url = url[1] + "/";
		//Get Thumbnail
		var thumb_url = contents.match(/src\=\"(.*?)\"/);
		var thumb_url = thumb_url[1];
		//Title
		var title = contents.match(/\<a title\=\"(.*?)\"/);
		var title = ImproveText(title[1]);
		//Description
		var description = contents.match(/\<\/span\>(.*?)\<\/p\>/);
		var description = splitTo2Lines(ImproveText(description[1]), 60);

		info = new Object();
		info.URL = url;
		info.ThumbnailURL = thumb_url;
		info.Title = title;
		info.Description = description;
		info.SaveFilename = ToFilename(title);
		info.attr = 7;
		result.VideoInfo.push( info );
		if(result.VideoInfo.length == perpage){break;}
		if(result.start + (result.VideoInfo.length - 1) == result.total){break;}
	} else{
		break;
	}
	}


	if(result.VideoInfo.length) {
		result.end = result.start + (result.VideoInfo.length - 1);
	} else {
		result.total = 0;	// no results.
	}

	if(result.VideoInfo.length != perpage){result.total = result.end;}

	return result;
}

// Service metadata
var Revision3 = new Object();
Revision3.Name          = "Revision3";		/* Service name */
Revision3.Description   = "Revision3";		/* Service description */
Revision3.SearchDesc    = "Revision3";		/* Service search screen description */
Revision3.SearchOSKMode = 1;			/* OSK mode 1: English only  2: Japanese */
Revision3.CheckURL      = Revision3_CheckURL;	/* CheckURL function */
Revision3.GetURL        = Revision3_GetURL;	/* GetURL function */
Revision3.Search        = Revision3_Search;	/* Search function */

// add Revision3 to site list
SiteList.push( Revision3 );
